package org.joyflow.core.meta;

import org.springframework.util.StringUtils;

public class H2DatabaseMeta extends BaseDatabaseMeta {

    @Override
    public String getDriverClass() {
        return "org.h2.Driver";
    }

    @Override
    public int getDefaultDatabasePort() {
        if (getAccessType() == TYPE_ACCESS_NATIVE) {
            return 8082;
        }
        return -1;
    }

    @Override
    public String getFieldDefinition(FieldDefinition fieldDefinition) {
        StringBuilder retVal = new StringBuilder();
        String name = fieldDefinition.getName();
        int length = fieldDefinition.getLength();
        int precision = fieldDefinition.getPrecision();
        retVal.append(name).append(" ");
        int type = fieldDefinition.getType();
        switch (type) {
            case ValueMetaBase.TYPE_TIMESTAMP:
            case ValueMetaBase.TYPE_DATE:
                retVal.append("timestamp");
                break;
            case ValueMetaBase.TYPE_BOOLEAN:
                retVal.append("tinyint");
                break;
            case ValueMetaBase.TYPE_NUMBER:
            case ValueMetaBase.TYPE_INTEGER:
            case ValueMetaBase.TYPE_BIGNUMBER:
                if (fieldDefinition.getLength() > 0) {
                    if (precision > 0 || length > 18) {
                        retVal.append("decimal(").append(length).append(",").append(" ").append(precision).append(")");
                    } else {
                        if (length > 9) {
                            retVal.append("bigint");
                        } else if (length > 4) {
                            retVal.append("integer");
                        } else if (length > 1) {
                            retVal.append("smallint");
                        } else {
                            retVal.append("tinyint");
                        }
                    }
                } else {
                    retVal.append("double");
                }
                break;
            case ValueMetaBase.TYPE_STRING:
                if (length >= 5000) {
                    retVal.append("text");
                } else {
                    retVal.append("varchar").append("(").append(length > 0 ? length : 50).append(")");
                }
                break;
            default:
                retVal.append("unknown");
                break;
        }
        return retVal.toString();
    }

    @Override
    public String getURL(String hostname,String port,String databaseName) {
        if (getAccessType() == TYPE_ACCESS_NATIVE) {
            if ((databaseName != null && databaseName.startsWith("mem:")) || ((StringUtils.hasText(port) || "-1".equals(port)) && StringUtils.hasText(hostname))) {
                return "jdbc:h2:" + databaseName;
            } else {
                return "jdbc:h2:tcp://" + hostname + ":" + port + "/" + databaseName;
            }
        }
        throw new UnsupportedOperationException("Not Implement");
    }
}
